<?php
/*
Mongoplayer
FLV player for embedding in a browser
Creates a swf on the fly with controls

Version: 0.1 alpha
License: GPL

See README for more info and examples
*/
   class oFlv {
      public $filename;
      public $width;
      public $height;
      protected $movie;
      protected $vheight;
      protected $vwidth;
      protected $flags;
      protected $buttoncount=0;
      protected $debug=false;
         
      public function __construct($filename, $width, $height, $version=7) {
         $this->filename=$filename;
         $this->width=$width;
         $this->height=$height;
         $this->flags = (SWFBUTTON_UP | SWFBUTTON_HIT | SWFBUTTON_OVER | SWFBUTTON_DOWN);
         ming_useswfversion($version);
         $this->movie=new SWFMovie($version);
         $this->movie->setDimension($this->width,$this->height);
         $this->movie->setBackground(255,255,255);
         $this->add_button("seek","images/player_start.dbl","0");
         $this->add_button("play","images/player_play.dbl","_global.streamUrl");
         $this->add_button("pause","images/player_pause.dbl");
         $this->add_button("seek","images/player_end.dbl","_global.duration");
         $this->add_slider();
         
         $this->add_stream();
         $this->add_timer();
      }
      
      protected function add_stream() {
         $this->vheight=($this->height - 32);
         $this->vwidth=(($this->height - 17) / $this->height) * $this->width;
         //$this->vwidth=$this->width -32;
         $strAction="
            stop();
            netConn=new NetConnection();
            netConn.connect(null);
            _global.stream=new NetStream(netConn);
            video.attachVideo(_global.stream);
            _global.stream.setBufferTime(10);
            _global.streamUrl='".$this->filename."';
            _global.stream.play(_global.streamUrl);
            
            _global.stream.onMetaData = updateMetaData;
            function updateMetaData(obj) {
              _global.duration = obj.duration;
              _global.streamLen=obj.duration;
              _global.totalminutes=Math.floor(_global.duration/60);
              _global.totalseconds=Math.floor(_global.duration%60);
              if (_global.totalseconds<10) {
                  _global.totalseconds = '0'+_global.totalseconds;
               }
            }
            
            var interval = setInterval(update_status,200,_global.stream);
            function update_status(ns) {
               pctLoaded = Math.round(ns.bytesLoaded/ns.bytesTotal*100);
            
               kbLoaded = Math.round(ns.bytesLoaded/1000);
               kbTotal = Math.round(ns.bytesTotal/1000);
               pctLoaded = Math.round(kbLoaded/kbTotal*100);
               
               
            
               pct = Math.min(Math.round(ns.bufferLength/ns.bufferTime*100), 100);
              
               
               ns_seconds = ns.time;
               minutes = Math.floor(ns_seconds/60);
               seconds = Math.floor(ns_seconds%60);
               if (seconds<10) {
                  seconds = '0'+seconds;
               }
               
               progressbar(Math.round(kbLoaded/kbTotal*80));
               
               video_txt.text=minutes+':'+seconds+'/'+_global.totalminutes+':'+_global.totalseconds+' '+pctLoaded+'%';
            }
            this.createEmptyMovieClip('MCProgress',this.getNextHighestDepth());
            MCProgress.lineStyle(1, 0xFF0000, 0);
	         MCProgress.beginFill(0xFF0000);
	         MCProgress.moveTo(0, 0);
	         MCProgress.lineTo(150, 0);
	         MCProgress.lineTo(150, 1);
	         MCProgress.lineTo(0, 1);
	         MCProgress.lineTo(0, 0);
	         MCProgress.endFill();
	         MCProgress._x=90;
	         MCProgress._y=131;
	         MCProgress._width=0;
	         
            function progressbar(width) {
               MCProgress._width=width;
            }            
            
            MCProgress.swapDepths(slider);
            
            ";
         $stream=new SWFVideoStream();
         $stream->setDimension($this->vwidth,$this->vheight);
         $item=$this->movie->add($stream);
         $item->moveTo(0,0);
         $item->setname("video");
         $this->movie->add(new SWFAction($strAction));
      }
      
      protected function add_button($action,$icon,$target="") {
         $button = new SWFButton();
  	      $button->addShape($this->make_shape($icon), $this->flags);
  	      
  	      $action=new SWFAction("_global.stream.".$action."($target);");
 	      $button->addAction($action, SWFBUTTON_MOUSEDOWN);
	      $button_ref = $this->movie->add($button);
	      $button_ref->moveTo($this->buttoncount*17, $this->height - 31);
	      $this->buttoncount++;
      }
      
      protected function add_slider() {
         $x=90;
         $maxwidth=80;
         $y=$this->height-25;
         $img = new SWFBitmap(fopen("images/slider1.dbl", "rb"));
	      $slider = $this->movie->add($img);
	      $slider->moveTo($x, $y);
         $mc = new SWFSprite();
	      /*$shape = new SWFShape();
         $shape->setLine(4,25,0,0,128);
         $shape->movePenTo(0, 5);
         $shape->drawLineTo(0, 10);*/
         $mc->add($this->make_shape("images/timecursor.dbl"));
	      $mc->nextFrame();
	      $slider = $this->movie->add($mc);
	      $slider->moveTo($x, $y);
	      $slider->setName("slider");
	      $slider_action = "
		      paused = false;
            if(drag) { 
			      _global.stream.pause();
			      paused = true;
               x = _root._xmouse - $x;
			      seekTo = (_global.duration / $maxwidth) * x;
			      _global.stream.seek(seekTo);
            } else {
			      mult = Math.min(1, ($maxwidth) / _global.duration);
               pos = (_global.stream.time / _global.duration * $maxwidth)+$x; 
               this._x = pos; 
               this._y = $y; 
            }
		      if(paused) {
			      _global.stream.pause();	
		      }
		      
		   ";
		   $action = new SWFAction($slider_action);
	      $slider->addAction($action, SWFACTION_ENTERFRAME);
	      $action = new SWFAction("startDrag(1, $x, $y, ($maxwidth+$x), $y, 1); drag = true;"); 
	      $slider->addAction($action, SWFACTION_MOUSEDOWN);
	      $action = new SWFAction("stopDrag(); drag=flase;");
	      $slider->addAction($action, SWFACTION_MOUSEUP);
      }
      
      protected function add_timer() {
         $meta_action="
            this.createTextField('video_txt', 999, 0, 132, 50, 50);
            video_txt.autoSize = 'left';
            
            video_txt.multiline = true;
            video_txt.textColor = 0x595959;
            ";
         $this->movie->add(new SWFAction($meta_action));
      }
      
      protected function make_shape($icon) {
      	$img = new SWFBitmap(fopen($icon, "rb"));
      	$w = $img->getWidth();
      	$h = $img->getHeight();
      	$shape = new SWFShape(); 
      	$shape->setLine(1,255,255,255,0); 
      	$fill = $shape->addFill($img, SWFFILL_TILED_BITMAP);  
      	$shape->setRightFill($fill);
      	$shape->drawLine($w,0); 
      	$shape->drawLine(0,$h); 
      	$shape->drawLine(-$w,0); 
      	$shape->drawLine(0,-$h); 
      	return $shape;
   }
      
      public function play() {
         $this->movie->nextFrame();
         $this->movie->output();
      }
      
      public function header() {
         header("Content-Type:application/x-shockwave-flash");
      }
      
      public function debug() {
         $this->debug=true;
         $str_action="
            this.createTextField('video_txt', 999, 0, 0, 100, 100);
            video_txt.autoSize = 'left';
            video_txt.multiline = true;
            video_txt.textColor = 0xeeeeee;
            
         ";
         $this->movie->add(new SWFAction($str_action));
      }
      
      protected function add_debug_text($str) {
         if ($this->debug) {
            $str_action="
               video_txt.text='$str';
               ";
            $this->movie->add(new SWFAction($str_action));
         }
      }
   }

?>